package com.smartao.base.employee;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.ForeignKey;

import com.smartao.base.department.Department;
import com.smartao.base.position.Position;
import com.smartao.base.role.Role;
import com.smartao.core.orm.entity.BaseEntity;
import com.smartao.core.security.SmartaoUser;

/**
 * 员工实体类
 * @author sunxing
 */
@Entity
@Table(name = "T_BASE_EMPLOYEE")
public class Employee extends BaseEntity<Employee> implements SmartaoUser {

	private static final long serialVersionUID = 4727358271545436027L;

	// 登录帐号
	@Column(nullable = false, unique = true, length = 128)
	private String account;
	private String password;
	//账号状态
	@Enumerated(EnumType.STRING)
	private AccountStatus accountStatus = AccountStatus.active;
	// 姓名
	private String name;
	// 拼音(根据姓名自动生成，用于查询，不显示)
	private String pinyin;
	// 编号(工号)
	private String code;
	// 英文名称
	private String englishName;
	
	@Enumerated(EnumType.STRING)
	private Sex sex;
	// （办公室）办公地点
	private String office;
	private String mobile;
	private String telephone;
	private String fax;
	private String email;
	// 即时通讯账号
	private String im;

	@ManyToOne
	@ForeignKey(name = "none")
	@JoinColumn(name = "avatar")
	@Fetch(FetchMode.JOIN)
	private Avatar avatar;
	// 显示顺序
	private Integer dsporder;
	// 部门
	@ManyToOne
	@ForeignKey(name = "none")
	@JoinColumn(name = "department")
	@Fetch(FetchMode.JOIN)
	private Department department;

	@ManyToOne
	@ForeignKey(name = "none")
	@JoinColumn(name = "manager")
	@Fetch(FetchMode.JOIN)
	private Employee manager;

	// 职位(主岗,冗余字段，方便快速查询)
	@ManyToOne
	@ForeignKey(name = "none")
	@JoinColumn(name = "position")
	@Fetch(FetchMode.JOIN)
	private Position position;

	// 所属岗位列表（包含了主岗和兼岗）
	@ManyToMany(targetEntity = Position.class, fetch = FetchType.LAZY)
	@JoinTable(name = "T_BASE_EMPLOYEE_POSITION", joinColumns = { @JoinColumn(name = "EMPLOYEE_ID", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "POSITION_ID", nullable = false) })
	@ForeignKey(name = "none", inverseName = "none")
	private Set<Position> positions;

	@ManyToMany(targetEntity = Role.class, fetch = FetchType.LAZY)
	@JoinTable(name = "T_BASE_EMPLOYEE_ROLE", joinColumns = { @JoinColumn(name = "EMPLOYEE_ID", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID", nullable = false) })
	@ForeignKey(name = "none", inverseName = "none")
	private Set<Role> roles;
	
	//安全级别
	private Long secLevel;

	@Override
	public String getAccount() {
		return account;
	}

	@Override
	public String getPassword() {
		return password;
	}

	public AccountStatus getAccountStatus() {
		return accountStatus;
	}

	public void setAccountStatus(AccountStatus accountStatus) {
		this.accountStatus = accountStatus;
	}

	@Override
	@Transient
	public List<String> getAuthorities() {
		if (getRoles() == null)
			return null;
		List<String> authorities = new ArrayList<String>();
		for (Role role : getRoles()) {
			authorities.add(role.getRoleKey());
		}
		return authorities;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPinyin() {
		return pinyin;
	}

	public void setPinyin(String pinyin) {
		this.pinyin = pinyin;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getEnglishName() {
		return englishName;
	}

	public void setEnglishName(String englishName) {
		this.englishName = englishName;
	}

	public Sex getSex() {
		return sex;
	}

	public void setSex(Sex sex) {
		this.sex = sex;
	}

	public String getOffice() {
		return office;
	}

	public void setOffice(String office) {
		this.office = office;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getFax() {
		return fax;
	}

	public void setFax(String fax) {
		this.fax = fax;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getIm() {
		return im;
	}

	public void setIm(String im) {
		this.im = im;
	}

	public Avatar getAvatar() {
		return avatar;
	}

	public void setAvatar(Avatar avatar) {
		this.avatar = avatar;
	}

	public Integer getDsporder() {
		return dsporder;
	}

	public void setDsporder(Integer dsporder) {
		this.dsporder = dsporder;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}

	public Set<Position> getPositions() {
		return positions;
	}

	public void setPositions(Set<Position> positions) {
		this.positions = positions;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Set<Role> getRoles() {
		return roles;
	}

	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	public void addRole(Role role) {
		if (this.roles == null) {
			this.roles = new HashSet<Role>();
		}
		if (!this.roles.contains(role)) {
			this.roles.add(role);
		}
	}

	public Employee getManager() {
		return manager;
	}

	public void setManager(Employee manager) {
		this.manager = manager;
	}
	
	public Long getSecLevel() {
		return secLevel;
	}

	public void setSecLevel(Long secLevel) {
		this.secLevel = secLevel;
	}

	@Override
	public String toString() {
		return name;
	}
	
	@Override
	@Transient
	public List<Long> getSids(){
		List<Long> sids = new ArrayList<Long>();
		sids.add(this.getId());
		for(Role role : getRoles()){
			sids.add(role.getId());
		}
		return sids;
	}

}
